package com.tianjipms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianjipms.entity.Feedback;
import com.tianjipms.entity.Opinion;
import com.tianjipms.service.FeedbackService;
import com.tianjipms.service.OpinionService;
import com.tianjipms.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/api/feedback")
public class FeedbackController {
    @Resource
    private FeedbackService feedbackService;
    @Resource
    private OpinionService opinionService;
    //回复意见
    @PostMapping("/add")
    public Result addFeedback(@RequestBody Feedback feedback){
        feedback.setStatus(0);
        feedback.setHandledTime(new Date());
        feedbackService.save(feedback);
        Opinion opinion = opinionService.getById(feedback.getOpinionId());
        opinionService.updateStatusById(opinion);
        return Result.ok();
    }
    //删除反馈
    @DeleteMapping("/delete/{id}")
    public Result deleteFeedback(@PathVariable int id) {
        if (feedbackService.removeById(id)){
            return Result.ok().message("反馈删除成功");
        }
        return Result.error().message("反馈删除失败");
    }
    //按id查询投诉
    @GetMapping("/one")
    public Result listByUserId(@RequestParam("opinionId") Long opinionId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("opinion_id", opinionId);
        return Result.ok(feedbackService.getOne(queryWrapper));
    }
}
